#
# Copyright (C) 2006-2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk

PKG_NAME:=cups_lede
PKG_VERSION:=2.4.12
PKG_RELEASE:=2

PKG_MAINTAINER:=Tobias Waldvogel <tobias.waldvogel@gmail.com>
PKG_SOURCE_NAME:=cups
PKG_SOURCE:=$(PKG_SOURCE_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://github.com/OpenPrinting/cups/archive/refs/tags/
PKG_HASH:=7a4d32822b320aa2999b18fdfc4ce5ca9ad204fe6302ff69e6c24b21f8d0eaa0
PKG_LICENSE:=GPL-2.0
PKG_LICENSE_FILES:=LICENSE.txt

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SOURCE_NAME)-$(PKG_VERSION)

PKG_FIXUP:=autoreconf
PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=0

include $(INCLUDE_DIR)/package.mk

define Package/cups_lede/Default
  URL:=http://www.cups.org/
endef

define Package/cups_lede
$(call Package/cups_lede/Default)
  SECTION:=utils
  SUBMENU:=Utilities
  CATEGORY:=Printing
  DEPENDS:=+libcups_lede +libusb-1.0 +libstdcpp +libubox +libubus +umdns
  TITLE:=(LEDE) Common UNIX Printing System (daemon)
endef

define Package/cups_lede/description
	LEDE Common UNIX Printing System (daemon)
endef

define Package/cups_lede/conffiles
/etc/cups/classes.conf
/etc/cups/cupsd.conf
/etc/cups/printers.conf
endef

define Package/cups-bsd_lede
$(call Package/cups_lede/Default)
  SECTION:=utils
  SUBMENU:=Utilities
  CATEGORY:=Printing
  DEPENDS:=+libcups_lede
  TITLE:=(LEDE) Common UNIX Printing System - BSD commands (old)
endef

define Package/cups-bsd_lede/description
	LEDE Common UNIX Printing System - BSD commands (old)
endef

define Package/cups-client_lede
$(call Package/cups_lede/Default)
  SECTION:=utils
  SUBMENU:=Utilities
  CATEGORY:=Printing
  DEPENDS:=+libcups_lede +libcupsimage_lede
  TITLE:=(LEDE) Common UNIX Printing System - Client commands
endef

define Package/cups-client_lede/conffiles
/etc/cups/client.conf
endef

define Package/cups-client_lede/description
	LEDE Common UNIX Printing System - Client commands
endef

define Package/libcups_lede
$(call Package/cups_lede/Default)
  SECTION:=libs
  SUBMENU:=Libraries
  CATEGORY:=Printing
  DEPENDS:=+zlib +libpthread +libpng +libjpeg +libubox +libubus
  TITLE:=(LEDE) Common UNIX Printing System - Core library
endef

define Package/libcups_lede/description
	LEDE Common UNIX Printing System - Core library
endef

define Package/libcupsimage_lede
$(call Package/cups_lede/Default)
  SECTION:=libs
  SUBMENU:=Libraries
  CATEGORY:=Printing
  DEPENDS:=+libcups_lede
  TITLE:=(LEDE) Common UNIX Printing System - Image library
endef

define Package/libcupsimage_lede/description
	LEDE Common UNIX Printing System - Image library
endef

CONFIGURE_ARGS += \
		--libdir=/usr/lib \
		--with-languages="all" \
		--with-cups-user="nobody" \
		--with-cups-group="nogroup" \
		--with-system-groups="root" \
		--enable-default-shared \
		--enable-shared \
		--enable-libusb \
		--disable-dbus \
		--with-dnssd=no \
		--with-tls=no \
		--disable-acl \
		--disable-pam \
		--disable-gssapi \
		--without-perl \
		--without-python

CONFIGURE_VARS += \
		UNAME="Linux" \
		LIBS="$(TARGET_LDFLAGS) -lz -lpng -ljpeg -lubus -lubox"

MAKE_VARS += \
		STRIPPROG="/bin/true" \
		CHMODPROG="/bin/true" \
		CHGRPPROG="/bin/true" \
		CHOWNPROG="/bin/true"

EXTRA_CFLAGS+=-Wno-sign-conversion -DHAVE_UBUS
TARGET_LDFLAGS+=-Wl,-rpath-link=$(STAGING_DIR)/usr/lib

define InstallDir
	$(INSTALL_DIR) $(2)$(3)
	$(INSTALL_$(1)) \
		$(foreach pat,$(4),$(if $(5),$(5),$(PKG_INSTALL_DIR))$(3)/$(pat)) \
		$(2)$(3)/
endef

define InstallDirLib
	$(INSTALL_DIR) $(1)$(2)
	$(CP) $(PKG_INSTALL_DIR)$(2)/$(3) $(1)$(2)/
endef

define InstallLink
	$(foreach pat,$(3),$(LN) $(2) $(1)/$(pat); )
endef

# define Package/cups-locale_lede-de
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale de
# endef

# define Package/cups-locale_lede-de/description
# 	LEDE Common UNIX Printing System - Locale de
# endef

# define Package/cups-locale_lede-nl
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale nl
# endef

# define Package/cups-locale_lede-nl/description
# 	LEDE Common UNIX Printing System - Locale nl
# endef

# define Package/cups-locale_lede-hu
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale hu
# endef

# define Package/cups-locale_lede-hu/description
# 	LEDE Common UNIX Printing System - Locale hu
# endef

# define Package/cups-locale_lede-pt
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale pt
# endef

# define Package/cups-locale_lede-pt/description
# 	LEDE Common UNIX Printing System - Locale pt
# endef

# define Package/cups-locale_lede-pt_BR
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale pt_BR
# endef

# define Package/cups-locale_lede-pt_BR/description
# 	LEDE Common UNIX Printing System - Locale pt_BR
# endef

# define Package/cups-locale_lede-fi
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale fi
# endef

# define Package/cups-locale_lede-fi/description
# 	LEDE Common UNIX Printing System - Locale fi
# endef

# define Package/cups-locale_lede-sv
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale sv
# endef

# define Package/cups-locale_lede-sv/description
# 	LEDE Common UNIX Printing System - Locale sv
# endef

# define Package/cups-locale_lede-da
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale da
# endef

# define Package/cups-locale_lede-da/description
# 	LEDE Common UNIX Printing System - Locale da
# endef

# define Package/cups-locale_lede-ko
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale ko
# endef

# define Package/cups-locale_lede-ko/description
# 	LEDE Common UNIX Printing System - Locale ko
# endef

define Package/cups-locale_lede-zh
$(call Package/cups_lede/Default)
  SECTION:=utils
  SUBMENU:=Utilities
  CATEGORY:=Printing
  TITLE:=(LEDE) Common UNIX Printing System - Locale zh
endef

define Package/cups-locale_lede-zh/description
	LEDE Common UNIX Printing System - Locale zh
endef

# define Package/cups-locale_lede-ja
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale ja
# endef

# define Package/cups-locale_lede-ja/description
# 	LEDE Common UNIX Printing System - Locale ja
# endef

# define Package/cups-locale_lede-es
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale es
# endef

# define Package/cups-locale_lede-es/description
# 	LEDE Common UNIX Printing System - Locale es
# endef

# define Package/cups-locale_lede-zh_TW
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale zh_TW
# endef

# define Package/cups-locale_lede-zh_TW/description
# 	LEDE Common UNIX Printing System - Locale zh_TW
# endef

# define Package/cups-locale_lede-pl
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale pl
# endef

# define Package/cups-locale_lede-pl/description
# 	LEDE Common UNIX Printing System - Locale pl
# endef

# define Package/cups-locale_lede-no
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale no
# endef

# define Package/cups-locale_lede-no/description
# 	LEDE Common UNIX Printing System - Locale no
# endef

# define Package/cups-locale_lede-ru
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale ru
# endef

# define Package/cups-locale_lede-ru/description
# 	LEDE Common UNIX Printing System - Locale ru
# endef

# define Package/cups-locale_lede-eu
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale eu
# endef

# define Package/cups-locale_lede-eu/description
# 	LEDE Common UNIX Printing System - Locale eu
# endef

# define Package/cups-locale_lede-fr
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale frgithub.com.cnpmjs.org
# endef

# define Package/cups-locale_lede-fr/description
# 	LEDE Common UNIX Printing System - Locale fr
# endef

# define Package/cups-locale_lede-it
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale it
# endef

# define Package/cups-locale_lede-it/description
# 	LEDE Common UNIX Printing System - Locale it
# endef

# define Package/cups-locale_lede-id
# $(call Package/cups_lede/Default)
#   SECTION:=net
#   CATEGORY:=Network
#   TITLE:=(LEDE) Common UNIX Printing System - Locale id
# endef
# define Package/cups-locale_lede-id/description
# 	LEDE Common UNIX Printing System - Locale id
# endef

# define Package/cups-locale_lede-de/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/de $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/de \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-nl/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/nl $(1)/usr/share/locale/
# endef

# define Package/cups-locale_lede-hu/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/hu $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/hu \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-pt/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/pt $(1)/usr/share/locale/
# endef

# define Package/cups-locale_lede-pt_BR/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/pt_BR $(1)/usr/share/locale/
# endef

# define Package/cups-locale_lede-fi/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/fi $(1)/usr/share/locale/
# endef

# define Package/cups-locale_lede-sv/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/sv $(1)/usr/share/locale/
# endef

# define Package/cups-locale_lede-da/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/da $(1)/usr/share/locale/
# endef

# define Package/cups-locale_lede-ko/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/ko $(1)/usr/share/locale/
# endef

define Package/cups-locale_lede-zh/install
	$(INSTALL_DIR) $(1)/usr/share/locale
	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/zh_CN $(1)/usr/share/locale/
endef

# define Package/cups-locale_lede-ja/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/ja $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/ja \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-es/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/es $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/es \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-zh_TW/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/zh_TW $(1)/usr/share/locale/
# endef

# define Package/cups-locale_lede-pl/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/pl $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/pl \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-no/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/no $(1)/usr/share/locale/
# endef

# define Package/cups-locale_lede-ru/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/ru $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/ru \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-eu/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/eu $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/eu \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-fr/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/fr $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/fr \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-it/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/it $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/it \
# 		$(1)/usr/share/cups_lede/templates/
# endef

# define Package/cups-locale_lede-id/install
# 	$(INSTALL_DIR) $(1)/usr/share/locale
# 	$(INSTALL_DIR) $(1)/usr/share/cups_lede/templates
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/locale/id $(1)/usr/share/locale/
# 	$(CP) $(PKG_INSTALL_DIR)/usr/share/cups_lede/templates/id \
# 		$(1)/usr/share/cups_lede/templates/
# endef

define Package/cups_lede/install
	$(call InstallDir,DATA,$(1),/etc/cups,*.conf*)
	$(INSTALL_DATA) ./files/etc/cups/* $(1)/etc/cups/
	$(LN) /tmp/cups $(1)/etc/cups/certs

	$(call InstallDir,BIN,$(1),/usr/sbin,cupsctl cupsd)

	$(call InstallDir,BIN,$(1),/usr/lib/cups/backend,*)
	$(call InstallDir,BIN,$(1),/usr/lib/cups/command,*)
	$(call InstallDir,BIN,$(1),/usr/lib/cups/cgi-bin,*)
	$(call InstallDir,BIN,$(1),/usr/lib/cups/daemon,*)
	$(call InstallDir,BIN,$(1),/usr/lib/cups/monitor,*)
	$(call InstallDir,BIN,$(1),/usr/lib/cups/filter,*)
	# needed for cups to find usb printers per http://wiki.openwrt.org/doc/howto/cups.server
	chmod 700 $(1)/usr/lib/cups/backend/usb

	$(INSTALL_DIR) $(1)/usr/share/cups/banners
	# Web interface
	$(call InstallDir,DATA,$(1),/usr/share/cups/templates,*.tmpl)
	$(call InstallDir,DATA,$(1),/usr/share/cups/mime,*)
	$(call InstallDir,DATA,$(1),/usr/share/doc/cups,*.html *.css *.txt)
	$(call InstallDir,DATA,$(1),/usr/share/doc/cups/images,*)
	# Init file
	$(call InstallDir,BIN,$(1),/etc/init.d,cupsd,files)
endef

define Package/cups-bsd_lede/install
	$(call InstallDir,BIN,$(1),/usr/bin,lprm lpq lpr)
	$(call InstallDir,BIN,$(1),/usr/sbin,lpc)
endef

define Package/cups-client_lede/install
	$(call InstallDir,BIN,$(1),/usr/bin,lp cancel cupstestppd ipptool lpoptions lpstat)
	$(call InstallDir,BIN,$(1),/usr/sbin,cupsaccept cupsfilter lpadmin lpinfo lpmove)
	$(call InstallLink,$(1)/usr/sbin,cupsaccept,accept cupsenable,cupsdisable,reject,cupsreject)
	$(call InstallDir,DATA,$(1),/usr/share/cups/ipptool,*.test)
endef

define Package/libcups_lede/install
	$(call InstallDir,BIN,$(1),/usr/lib,libcups.so.2)
	$(call InstallLink,$(1)/usr/lib,libcups.so.2,libcups.so)
endef

define Package/libcupsimage_lede/install
	$(call InstallDir,BIN,$(1),/usr/lib,libcupsimage.so.2)
	$(call InstallLink,$(1)/usr/lib,libcupsimage.so.2,libcupsimage.so)
endef

define Build/InstallDev
	$(INSTALL_DIR) $(2)/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/cups-config $(2)/bin/
	$(INSTALL_DIR) $(1)/usr/include
	$(CP) $(PKG_INSTALL_DIR)/usr/include/cups $(1)/usr/include/
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib*/libcups*.* $(1)/usr/lib/
endef

$(eval $(call BuildPackage,cups_lede))
$(eval $(call BuildPackage,libcups_lede))
$(eval $(call BuildPackage,libcupsimage_lede))
$(eval $(call BuildPackage,cups-bsd_lede))
$(eval $(call BuildPackage,cups-client_lede))
# $(eval $(call BuildPackage,cups-locale_lede-de))
# $(eval $(call BuildPackage,cups-locale_lede-nl))
# $(eval $(call BuildPackage,cups-locale_lede-hu))
# $(eval $(call BuildPackage,cups-locale_lede-pt))
# $(eval $(call BuildPackage,cups-locale_lede-pt_BR))
# $(eval $(call BuildPackage,cups-locale_lede-fi))
# $(eval $(call BuildPackage,cups-locale_lede-sv))
# $(eval $(call BuildPackage,cups-locale_lede-da))
# $(eval $(call BuildPackage,cups-locale_lede-ko))
$(eval $(call BuildPackage,cups-locale_lede-zh))
# $(eval $(call BuildPackage,cups-locale_lede-ja))
# $(eval $(call BuildPackage,cups-locale_lede-es))
# $(eval $(call BuildPackage,cups-locale_lede-zh_TW))
# $(eval $(call BuildPackage,cups-locale_lede-pl))
# $(eval $(call BuildPackage,cups-locale_lede-no))
# $(eval $(call BuildPackage,cups-locale_lede-ru))
# $(eval $(call BuildPackage,cups-locale_lede-eu))
# $(eval $(call BuildPackage,cups-locale_lede-fr))
# $(eval $(call BuildPackage,cups-locale_lede-it))
# $(eval $(call BuildPackage,cups-locale_lede-id))



